package com.xiong.word.utils;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class WordUtils {

    public static void setValue(XWPFTableCell cell,String str){
        // 清空单元格中的所有内容
        for (int i = cell.getParagraphs().size() - 1; i >= 0; i--) {
            cell.removeParagraph(i);
        }
        // 添加新的文本内容
        XWPFParagraph newParagraph = cell.addParagraph();
        XWPFRun run = newParagraph.createRun();
        // 设置字体
        run.setFontFamily("新宋体");
        // 设置字号
        run.setFontSize(10);
        // 设置加粗
     //   run.setBold(true);

        run.setText(str);
    }

    public static void replaceVariable(XWPFTableCell  cell,String replaceStr,String textStr){
        for (XWPFParagraph paragraph : cell.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(run.getTextPosition());
                if (text != null && text.contains(replaceStr)) {
                    text = text.replace(replaceStr, textStr);
                    run.setText(text, 0);
                }
            }
        }
    }

    public static void replaceVariable(XWPFDocument document, String variable, String replacement) {
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                if (text != null && text.contains(variable)) {
                    text = text.replace(variable, replacement);
                    run.setText(text, 0);
                }
            }
        }
    }

    public static XWPFDocument getXWPFDocument(String path){
        try {
            FileInputStream template = new FileInputStream(path);
            XWPFDocument document = new XWPFDocument(template);
            return document;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    public static void insertRow(XWPFTable table, int copyrowIndex, int newrowIndex) {
        // 在表格中指定的位置新增一行
        XWPFTableRow targetRow = table.insertNewTableRow(newrowIndex);
        // 获取需要复制行对象
        XWPFTableRow copyRow = table.getRow(copyrowIndex);
        //复制行对象给新增的行对象
        targetRow.getCtRow().setTrPr(copyRow.getCtRow().getTrPr());
        //获取需要复制行对象的列
        List<XWPFTableCell> copyCells = copyRow.getTableCells();
        //新增的对象的列
        XWPFTableCell targetCell = null;
        //遍历复制行对象的列
        for (int i = 0; i < copyCells.size(); i++) {
            //复制行对象的列
            XWPFTableCell copyCell = copyCells.get(i);
            //新增的行对象创建一列
            targetCell = targetRow.addNewTableCell();
            //格式复制
            targetCell.getCTTc().setTcPr(copyCell.getCTTc().getTcPr());
            if (copyCell.getParagraphs() != null && copyCell.getParagraphs().size() > 0) {
                targetCell.getParagraphs().get(0).getCTP().setPPr(copyCell.getParagraphs().get(0).getCTP().getPPr());
                if (copyCell.getParagraphs().get(0).getRuns() != null
                        && copyCell.getParagraphs().get(0).getRuns().size() > 0) {
                    XWPFRun cellR = targetCell.getParagraphs().get(0).createRun();
                    cellR.setBold(copyCell.getParagraphs().get(0).getRuns().get(0).isBold());
                }
            }
        }
    }
}
